package com.kld.test;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.Log;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 时间工具类
 */
public class TimeUtils {

    /** 增加时间戳转换方法*/

    /**
     * 时:分
     */
    public static final String TEMPLATE_TIME = "HH:mm";
    /**
     * 年.月.日
     */
    public static final String TEMPLATE_DATE_DOT = "yyyy.MM.dd";
    /**
     * 年-月-日
     */
    public static final String TEMPLATE_DATE_DASH = "yyyy-MM-dd";
    /**
     * 年-月-日
     */
    public static final String TEMPLATE_DATE_DASH2 = "yyyy-M-d";
    /**
     * 年-月-日
     */
    public static final String TEMPLATE_DATE_CH = "yyyy年MM月dd日";
    /**
     * 年月日
     */
    public static final String TEMPLATE_DATE_STR = "yyyyMMdd";
    /**
     * 年月
     */
    public static final String TEMPLATE_DATE_STR_MONTH = "yyyyMM";
    /**
     * 年.月
     */
    public static final String TEMPLATE_DATE_STR_MONTH_DOT = "yyyy.MM";
    /**
     * 月/日 时:分
     */
    public static final String TEMPLATE_MONTH_DAY_HOUR = "MM/dd HH:mm";
    /**
     * 月/日
     */
    public static final String TEMPLATE_MONTH_DAY = "MM/dd";
    /**
     * 年.月.日 时:分
     */
    public static final String TEMPLATE_DATE_DOT_TIME = "yyyy.MM.dd HH:mm";
    /**
     * 年-月-日 时:分
     */
    public static final String TEMPLATE_DATE_DASH_TIME = "yyyy-MM-dd HH:mm";

    /**
     * 年-月-日 时:分:秒
     */
    public static final String TEMPLATE_DATE_DAY_ALL = "yyyy-MM-dd HH:mm:ss";


    /**
     * 年-月-日 时:分:秒
     */
    public static final String TEMPLATE_DATE_DASH_TIME_ALL = "yyyy年MM月dd日 HH:mm:ss";
    /**
     * 年-月-日 时:分:秒
     */
    public static final String TEMPLATE_DATE_CH_HM = "yyyy年M月d日 H:m";
    /**
     * 年-月
     */
    public static final String TEMPLATE_DATE_YEAR_MONTH_TIME = "yyyy年MM月";

    /**
     * 年-月
     */
    public static final String TEMPLATE_DATE_YEAR_MONTH_TIME2 = "yyyy-MM";
    /**
     * 年-月
     */
    public static final String TEMPLATE_DATE_YEAR_MONTH_TIME3 = "yyyy-M";
    /**
     * 年
     */
    public static final String TEMPLATE_DATE_YEAR = "yyyy";
    /**
     * 月
     */
    public static final String TEMPLATE_DATE_MONTH = "MM";
    /**
     * 日
     */
    public static final String TEMPLATE_DATE_DAY = "dd";
    /**
     * 时:分:秒
     */
    public static final String TEMPLATE_DATE_DAY_TIME = "HH:mm:ss";

    /**
     * 年-月-日 时:分
     */
    public static final String TEMPLATE_DATE_TIME = "yyyy年MM月dd日 HH:mm";

    /**
     * 年月日时分秒
     */
    public static final String TEMPLATE_DATE_TIME_BLANK = "yyMMddHHmmss";


    public static String getCurrenTime(String formatstr) {
        SimpleDateFormat format = new SimpleDateFormat(formatstr);
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String currenTime = format.format(curDate);
        return currenTime;
    }


    /**
     * 时间格式化
     *
     * @param millis
     * @param template
     * @return
     */
    public static String getTime(long millis, String template) {
//        if (millis <= 0) {
//            return "";
//        }
        String millisStr = millis + "";
        Date date;
        if (millisStr.length() < 13) {
            date = new Date(millis * 1000);
        } else {
            date = new Date(millis);
        }

        SimpleDateFormat format = new SimpleDateFormat(template, Locale.getDefault());

        return format.format(date);
    }

    public static String getTime3(String millis, String template) {
        if (StringUtils.isEmpty(millis)) {
            return "";
        }
        Date date = null;
        DateFormat df2 = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            date = df.parse(millis);
            df2 = new SimpleDateFormat(template);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df2.format(date);
    }

   

    /**
     * @param time
     * @param template
     * @return
     */
    public static String getTime(String time, String template) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(template, Locale.getDefault());
        Date date;
        try {
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = ft.parse(time);
        } catch (ParseException e) {
            return "";
        }

        return format.format(date);


    }

    public static long getTimestamp(String timeString, String template) {
        SimpleDateFormat format = new SimpleDateFormat(template, Locale.getDefault());
        Date d;
        try {
            d = format.parse(timeString);
            return d.getTime();
        } catch (ParseException e) {
            return 0L;
        }
    }


    /**
     * 计算新手任务到期时间差
     *
     * @return 返回时间差
     */
    public static String getTimeDifference(Context ctx, long createdTIme, long currentTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // long ns = 1000;
        // 获得两个时间的秒时间差异
        long endTime = createdTIme + 30 * 24 * 60 * 60 * 1000L;//注册时间30天后

        long diff = endTime - currentTime;
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        String res = "";
        if (day != 0) {
            res += day + "天";
        }
        res += hour + "小时" + min + "分钟" + sec + "秒";

        return res;

    }


    /**
     * 获取到时间戳
     *
     * @param time template返回时间格式
     * @return
     */
    public static long getLongTime(String time, String template) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(template);
        Date date = null;
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date != null) {
            return date.getTime();
        } else {
            return 0;
        }
    }


    public static String getTime(Date date, String pattern) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat(pattern);
//        format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return format.format(date);
    }


    /**
     * 刚发布 例如：刚刚
     * 仅一小时内显示分钟数，例如：xx分钟前；
     * 一小时以上，以小时为单位显示小时数，例如：x小时前；
     * 24小时以上以天为单位显示，例如：1天前；
     * 3天以上，取时间（Datetime），例如：2020.12.12 12:12:12
     * 超过3天时，显示月日（03-15），往年显示：年月日（2020-03-15）
     *
     * @param longTime
     * @return
     */
    public static String getTimeFormatExt(Context ctx, long longTime) {
        int daynum = 3;
        String str_time = longTime + "";
        if (str_time.length() < 13) {
            longTime = Long.valueOf(str_time + "000");
        } else {
            longTime = Long.valueOf(str_time);
        }
        String timestr = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(longTime);
        long lYear = calendar.get(Calendar.YEAR);
        Calendar currentCalendar = Calendar.getInstance();
        long lCurYear = currentCalendar.get(Calendar.YEAR);
        Timestamp time = new Timestamp(Long.valueOf(longTime));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat format1 = new SimpleDateFormat("MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = now.getTime() - time.getTime();
        long temp_conver;

        if (lCurYear == lYear) {
            if ((time_conver / day_conver) < daynum) {
                temp_conver = time_conver / day_conver;
                if (temp_conver <= daynum - 1 && temp_conver >= 1) {
                    return temp_conver + "天前";
                } else {
                    temp_conver = (time_conver / hour_conver);
                    if (temp_conver >= 1) {
                        return temp_conver + "小时前";
                    } else {
                        temp_conver = (time_conver / min_conver);
                        if (temp_conver >= 1) {
                            return temp_conver + "分钟前";
                        } else {
                            return "刚刚";
                        }
                    }
                }
            } else {
                timestr = format1.format(time);
                return timestr;
            }
        } else {
            timestr = format2.format(time);
            return timestr;
        }

    }

    /**
     * 根据时间戳来判断当前的时间是几天前,几分钟,刚刚
     *
     * @param long_time
     * @return
     */
    public static String getTimeFormat(Context ctx, long long_time) {
        int daynum = 3;
        String str_time = long_time + "";
        if (str_time.length() < 13) {
            long_time = Long.valueOf(str_time + "000");
        } else {
            long_time = Long.valueOf(str_time);
        }
        String timestr = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(long_time);
        long lYear = calendar.get(Calendar.YEAR);
        Calendar currentCalendar = Calendar.getInstance();
        long lCurYear = currentCalendar.get(Calendar.YEAR);
        Timestamp time = new Timestamp(Long.valueOf(long_time));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = now.getTime() - time.getTime();
        long temp_conver;
        if ((time_conver / day_conver) < daynum) {
            temp_conver = time_conver / day_conver;
            if (temp_conver <= daynum - 1 && temp_conver >= 1) {
                return temp_conver + "天前";
            } else {
                temp_conver = (time_conver / hour_conver);
                if (temp_conver >= 1) {
                    return temp_conver + "小时前";
                } else {
                    temp_conver = (time_conver / min_conver);
                    if (temp_conver >= 1) {
                        return temp_conver + "分钟前";
                    } else {
                        return "刚刚";
                    }
                }
            }
        } else {
            timestr = format.format(time);
            if (lCurYear == lYear) {
                timestr = timestr.substring(5, timestr.length());
            }
            return timestr;
        }
    }

    public static String getCalendarWeek2(Context ctx, int weekstr) {
        String weeks = "星期天";
        if (weekstr == 1) {
            weeks = "星期一";
        } else if (weekstr == 2) {
            weeks = "星期二";
        } else if (weekstr == 3) {
            weeks = "星期三";
        } else if (weekstr == 4) {
            weeks = "星期四";
        } else if (weekstr == 5) {
            weeks = "星期五";
        } else if (weekstr == 6) {
            weeks = "星期六";
        } else if (weekstr == 7) {
            weeks = "星期天";
        }


        return weeks;
    }

    public static String getCalendarWeek1(Context ctx, String weekstr) {
        String weeks = "星期天";
        if ("1".equals(weekstr)) {
            weeks = "星期天";
        } else if ("2".equals(weekstr)) {
            weeks = "星期一";
        } else if ("3".equals(weekstr)) {
            weeks = "星期二";
        } else if ("4".equals(weekstr)) {
            weeks = "星期三";
        } else if ("5".equals(weekstr)) {
            weeks = "星期四";
        } else if ("6".equals(weekstr)) {
            weeks = "星期五";
        } else if ("7".equals(weekstr)) {
            weeks = "星期六";
        }


        return weeks;
    }


    public static String getCalendarWeek(Context ctx, int weeksint) {
        String weekstr = "周日";
        if (weeksint == 1) {
            weekstr = "周一";
        } else if (weeksint == 2) {
            weekstr = "周二";
        } else if (weeksint == 3) {
            weekstr = "周三";
        } else if (weeksint == 4) {
            weekstr = "周四";
        } else if (weeksint == 5) {
            weekstr = "周五";
        } else if (weeksint == 6) {
            weekstr = "周六";
        } else if (weeksint == 7) {
            weekstr = "周日";
        }

        return weekstr;
    }


    /**
     * 时间戳格式化年月日周
     *
     * @param timestamp
     * @return
     */
    public static String getyyyyMMddWeekToTimestamp2(Context ctx, long timestamp) {
        String days = "";
        Calendar calendar = Calendar.getInstance();
        String millisStr = timestamp + "";
        if (millisStr.length() == 10) {
            timestamp = timestamp * 1000;
        }
        calendar.setTimeInMillis(timestamp);
        long lYear = calendar.get(Calendar.YEAR);
        int lMonth = calendar.get(Calendar.MONTH) + 1;
        int lDay = calendar.get(Calendar.DAY_OF_MONTH);
        String year = String.valueOf(lYear);
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String weekstr = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        Calendar currentCalendar = Calendar.getInstance();
        long lCurYear = currentCalendar.get(Calendar.YEAR);
        int curMonth = currentCalendar.get(Calendar.MONTH) + 1;
        int curDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
        if (lCurYear == lYear) {
            if (lMonth == curMonth && lDay == curDay) {
                days = "今天" + " " + month + "月" + day + "日" + " " + TimeUtils.getCalendarWeek1(ctx, weekstr);
            } else {
                days = month + "月" + day + "日" + " " + TimeUtils.getCalendarWeek1(ctx, weekstr);
            }

        } else {
            days = year + "年" + " " + month + "月" + day + "日" + " " + TimeUtils.getCalendarWeek1(ctx, weekstr);
        }

        return days;
    }

    /**
     * 时间戳格式化年月日周
     *
     * @param timestamp
     * @return
     */
    public static String getyyyyMMddWeekToTimestamp(Context ctx, long timestamp) {
        String days = "";
        Calendar calendar = Calendar.getInstance();
        String millisStr = timestamp + "";
        if (millisStr.length() == 10) {
            timestamp = timestamp * 1000;
        }
        calendar.setTimeInMillis(timestamp);
        long lYear = calendar.get(Calendar.YEAR);
        String year = String.valueOf(lYear);
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String weekstr = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        Calendar currentCalendar = Calendar.getInstance();
        long lCurYear = currentCalendar.get(Calendar.YEAR);

        if (lCurYear == lYear) {
            days = month + "月" + day + "日" + " " + TimeUtils.getCalendarWeek1(ctx, weekstr);
        } else {
            days = year + "年" + " " + month + "月" + day + "日" + " " + TimeUtils.getCalendarWeek1(ctx, weekstr);
        }

        return days;
    }


    /**
     * 时间戳格式化年月日周
     *
     * @param timestamp
     * @return
     */
    public static String getTimeHHmm(long timestamp) {
        String days = "";
        Calendar calendar = Calendar.getInstance();
        String millisStr = timestamp + "";
        if (millisStr.length() == 10) {
            timestamp = timestamp * 1000;
        }
        calendar.setTimeInMillis(timestamp);
        int lHour = calendar.get(Calendar.HOUR_OF_DAY);
        String hour = String.valueOf(lHour);
        if (lHour < 10) {
            hour = "0" + lHour;
        }
        int lMinute = calendar.get(Calendar.MINUTE);
        String minute = String.valueOf(lMinute);
        if (lMinute < 10) {
            minute = "0" + lMinute;
        }

        days = hour + ":" + minute;
        return days;
    }


    /**
     * 时间戳格式化年月日
     *
     * @param timestamp
     * @return
     */
    public static String getHHmmToTimestamp(long timestamp) {
        String days = "";
        Calendar calendar = Calendar.getInstance();
        String millisStr = timestamp + "";
        if (millisStr.length() == 10) {
            timestamp = timestamp * 1000;
        }
        calendar.setTimeInMillis(timestamp);
        int lHour = calendar.get(Calendar.HOUR_OF_DAY);
        String hour = String.valueOf(lHour);
        if (lHour < 10) {
            hour = "0" + lHour;
        }
        int lMinute = calendar.get(Calendar.MINUTE);
        String minute = String.valueOf(lMinute);
        if (lMinute < 10) {
            minute = "0" + lMinute;
        }
        days = hour + ":" + minute;

        return days;
    }

    public static boolean isToday(long millis) {
        boolean isToday = false;
        Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month + 1;
        int day = t.monthDay;
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(millis);
        int cmonth = cal.get(Calendar.MONTH) + 1;
        if (cal.get(Calendar.YEAR) == year && cmonth == month && cal.get(Calendar.DAY_OF_MONTH) == day) {
            isToday = true;
        } else {
            isToday = false;
        }
        return isToday;
    }


    public static String formatDateTime(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        if (days > 0) {
            DateTimes = days + " " + hours + ":" + minutesStr + "\'" + secondsStr + "\"";
        } else if (hours > 0) {
            DateTimes = hours + ":" + minutesStr + "\'" + secondsStr + "\"";
        } else if (minutes > 0) {
            DateTimes = minutesStr + "\'" + secondsStr + "\"";
        } else {
            DateTimes = "00\'" + secondsStr + "\"";
        }

        return DateTimes;
    }

    public static String formatDateTime2(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        if (minutes > 0) {
            DateTimes = minutesStr + "\'" + secondsStr + "\"";
        } else {
            DateTimes = "00:" + secondsStr + "\"";
        }

        return DateTimes;
    }

    public static String formatDateTime6(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        String hourStr = "";
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (hours < 10) {
            hourStr = "0" + hours;
        } else {
            hourStr = "" + hours;
        }
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        DateTimes = hourStr + ":" + minutesStr + ":" + secondsStr + "";
        return DateTimes;
    }


    public static String formatDateTime3(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        if (minutes > 0) {
            DateTimes = minutesStr + "\'" + secondsStr;
        } else {
            DateTimes = "00" + "\'" + secondsStr;
        }

        return DateTimes;
    }

    public static String formatDateTime5(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        if (minutes > 0) {
            DateTimes = minutesStr + "\'" + secondsStr + "\"";
        } else {
            DateTimes = "00" + "\'" + secondsStr + "\"";
        }

        return DateTimes;
    }

    public static String formatDateTime3EN(long mss) {
        //        long days = mss / (1000 * 60 * 60 * 24);
//        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = (mss % 60);
        String tmp_seconds = "";
        String tmp_minutes = "";
        if (seconds < 10) {
            tmp_seconds = "0" + seconds;
        } else {
            tmp_seconds = Long.toString(seconds);
        }
        if (minutes < 10) {
            if (mss >= 3600 && minutes == 0) {
                tmp_minutes = "60";
            } else {
                tmp_minutes = "0" + minutes;
            }

        } else {
            tmp_minutes = Long.toString(minutes);
        }
//        return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
        return tmp_minutes + ":" + tmp_seconds;

    }

    public static String formatDateTime3CN(Context ctx, long mss) {
        //        long days = mss / (1000 * 60 * 60 * 24);
//        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = (mss % 60);
        String tmp_seconds = "";
        String tmp_minutes = "";
        if (seconds < 10) {
            tmp_seconds = "0" + seconds;
        } else {
            tmp_seconds = Long.toString(seconds);
        }
        if (minutes < 10) {
            if (mss >= 3600 && minutes == 0) {
                tmp_minutes = "60";
            } else {
                tmp_minutes = "0" + minutes;
            }

        } else {
            tmp_minutes = Long.toString(minutes);
        }
//        return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
        return tmp_minutes + "分" + tmp_seconds + "秒";

    }

    /**
     * @param time
     * @desc 字符串转时间戳
     * @example time="2019-04-19"
     **/
    public static Long getTimestamp(String time) {
        Long timestamp = null;
        if (StringUtils.isNotEmpty(time)) {
            try {
                timestamp = new SimpleDateFormat("yyyy-MM-dd").parse(time).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return timestamp;
    }

    /**
     * 第当天某点的毫秒时间戳
     *
     * @return
     */
    public static Long getMillisTimestamp(int dayTime) {
        Calendar cal = Calendar.getInstance();
        //日期加1
//        cal.add(Calendar.DAY_OF_YEAR, 1);
        //时间设定到8点整
        cal.set(Calendar.HOUR_OF_DAY, dayTime);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获取当前周的所有日期
     *
     * @param date
     * @return
     */
    public static List<Date> getDateByWeek(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
//        String imptimeBegin = sdf.format(cal.getTime());
        Date timeBegin = cal.getTime();
        // System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
//        String imptimeEnd = sdf.format(cal.getTime());
        Date timeEnd = cal.getTime();
        // System.out.println("所在周星期日的日期：" + imptimeEnd);

        List<Date> lDate = new ArrayList<>();
        lDate.add(timeBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(timeBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(timeEnd);
        // 测试此日期是否在指定日期之后
        while (timeEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    /**
     * 根据已格式化过的字符串返回日期
     *
     * @param dateStr 20191226
     * @return
     */
    public static Date getDateByFormat(String dateStr) {
        SimpleDateFormat df = new SimpleDateFormat("yyMMdd");
        try {
            Date date = df.parse(dateStr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateYYYYMMToStamp(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


    /**
     * 获得时间加1天
     *
     * @return
     */
    public static String getTimesAddOneDay(String timeStr) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try {
            Date date = sf.parse(timeStr);
            cal.setTime(date);
            cal.add(Calendar.DAY_OF_MONTH, 1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sf.format(cal.getTime());
    }

    /**
     * 获得时间减1天
     *
     * @return
     */
    public static long getTimesLastDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTimeInMillis();
    }

    /**
     * 获得时间减1天
     *
     * @return
     */
    public static String getTimesSubOneDay(String timeStr) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try {
            Date date = sf.parse(timeStr);
            cal.setTime(date);
            cal.add(Calendar.DAY_OF_MONTH, -1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sf.format(cal.getTime());
    }


    public static int getAgeByBirth(long millis) {

        int age = 0;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());// 当前时间
            String millisStr = millis + "";
            Date date;
            if (millisStr.length() == 10) {
                date = new Date(millis * 1000);
            } else {
                date = new Date(millis);
            }
            Calendar birth = Calendar.getInstance();
            birth.setTime(date);

            if (birth.after(now)) {//如果传入的时间，在当前时间的后面，返回0岁
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                    age += 1;
                }
            }
            return age;
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return 0;
        }
    }

    public static String formatMinutesSecondsTime(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        long minutes = mss / 60;
        long seconds = mss % 60;
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes > 0) {
            DateTimes = minutesStr + "\'" + secondsStr + "\"";
        } else {
            DateTimes = "00\'" + secondsStr + "\"";
        }

        return DateTimes;
    }

    public static String formatMinutesSecondsTime3(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        long minutes = mss / 60;
        long seconds = mss % 60;
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes > 0) {
            DateTimes = minutesStr + "\'" + secondsStr;
        } else {
            DateTimes = "00\'" + secondsStr;
        }

        return DateTimes;
    }

    public static String formatMinutesSecondsTime2(long mss) {
        String DateTimes = null;
        String secondsStr = "";
        String minutesStr = "";
        long minutes = mss / 60;
        long seconds = mss % 60;
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = "" + minutes;
        }
        if (seconds < 10) {
            secondsStr = "0" + seconds;
        } else {
            secondsStr = "" + seconds;
        }
        if (minutes > 0) {
            DateTimes = minutesStr + ":" + secondsStr;
        } else {
            DateTimes = "00:" + secondsStr;
        }

        return DateTimes;
    }

    public static String getCalendarWeeks(Context ctx, int weeksint) {
        String weekstr = "周日";
        if (weeksint == 2) {
            weekstr = "周一";
        } else if (weeksint == 3) {
            weekstr = "周二";
        } else if (weeksint == 4) {
            weekstr = "周三";
        } else if (weeksint == 5) {
            weekstr = "周四";
        } else if (weeksint == 6) {
            weekstr = "周五";
        } else if (weeksint == 7) {
            weekstr = "周六";
        } else if (weeksint == 1) {
            weekstr = "周日";
        }

        return weekstr;
    }


    /**
     * 结束时间小于1小时，显示分（24分）
     * 时间小于1天，显示时分（23时15分）
     * 时间大于1天，显示天、时（3天5小时）
     *
     * @param long_time
     * @return
     */
    public static String getTimeFormatVote(long long_time) {
        String str_time = long_time + "";
        if (str_time.length() < 13) {
            long_time = Long.valueOf(str_time + "000");
        } else {
            long_time = Long.valueOf(str_time);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(long_time);
        Timestamp time = new Timestamp(Long.valueOf(long_time));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = time.getTime() - now.getTime();
        // 计算差多少天
        long day = time_conver / day_conver;
        // 计算差多少小时
        long hour = time_conver % day_conver / hour_conver;
        // 计算差多少分钟
        long min = time_conver % day_conver % hour_conver / min_conver;
        if (day >= 1) {
            return "还有" + day + "天" + hour + "小时结束";
        } else {
            if (hour >= 1) {
                return "还有" + hour + "时" + min + "分结束";
            } else {
                if (min > 0) {
                    return "还有" + min + "分结束";
                } else {
                    return "已结束";
                }

            }
        }

    }

    public static long getTimeFormatVoteEnd(long long_time) {
        String str_time = long_time + "";
        if (str_time.length() < 13) {
            long_time = Long.valueOf(str_time + "000");
        } else {
            long_time = Long.valueOf(str_time);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(long_time);
        Timestamp time = new Timestamp(Long.valueOf(long_time));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        long time_conver = time.getTime() - now.getTime();
        return time_conver;

    }

    public static long getTimeDiff(String startTime, String endTime) {
        DateFormat dft = new SimpleDateFormat(TEMPLATE_DATE_DAY_ALL);
        long days = 0l;
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            try {
                Date starDay = dft.parse(startTime);//开始时间
                Date endDay = dft.parse(endTime);//结束时间
                Long star = starDay.getTime();
                Long end = endDay.getTime();
                Long num = end - star;//时间戳相差的毫秒数
                days = num / 24 / 60 / 60 / 1000;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return days;
    }

    /**
     * @param inputJudgeDate 要判断是否在当天24h内的时间
     * @return boolean
     * @Description 是否为当天24h内
     */
    public static boolean isToday(String inputJudgeDate) {
        boolean flag = false;
        Date takeDate = null;
        //获取当前系统时间
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(nowDate);
        String subDate = format.substring(0, 10);
        //定义每天的24h时间范围
        String beginTime = subDate + " 00:00:00";
        String endTime = subDate + " 23:59:59";
        Date paseBeginTime = null;
        Date paseEndTime = null;
        try {
            paseBeginTime = dateFormat.parse(beginTime);
            paseEndTime = dateFormat.parse(endTime);
            takeDate = dateFormat.parse(inputJudgeDate);
        } catch (ParseException e) {
        }
        if (takeDate.after(paseBeginTime) && takeDate.before(paseEndTime)) {
            flag = true;
        }
        return flag;
    }
}
